package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.CntrType;
import com.hzhc.mapper.CntrTypeMapper;
import com.hzhc.service.CntrTypeService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.CntrTypeVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 箱类型代码 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@Service
@RequiredArgsConstructor
public class CntrTypeServiceImpl extends ServiceImpl<CntrTypeMapper, CntrType> implements CntrTypeService {

    @Override
    public List<CntrTypeVo> getCntrTypeList(Page<CntrTypeVo> page, CntrTypeVo vo) {
        IPage<CntrTypeVo> iPage = baseMapper.getCntrTypeList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addCntrType(CntrType type) {
        boolean b = save(type);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public Boolean checkTypeCode(CntrType type) {
        LambdaQueryWrapper<CntrType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CntrType::getTypeCode,type.getTypeCode())
                .eq(CntrType::getDisable,0);

        if (type.getId()!=null){
            wrapper.ne(CntrType::getId,type.getId());
        }
        int count = count(wrapper);
        return count <= 0;
    }

    @Override
    public ResultUtil<T> updateCntrType(CntrType type) {
        boolean b = updateById(type);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> delCntrType(Long id) {
        CntrType type = getById(id);
        type.setDisable(1);
        boolean b = updateById(type);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public List<CntrTypeVo> getCntrType(CntrTypeVo vo) {
        return baseMapper.getCntrType(vo);
    }

}
